<?php

declare(strict_types=1);

namespace App\Service\Business;

use App\Service\Access\RoleMenuAccess;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 角色菜单-业务类
 */
class RoleMenuBusiness extends BaseBusiness
{
        #[Inject]
        protected RoleMenuAccess $RoleMenuAccess;

        /**
         * 设定
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function setting(): array
        {
                $arrData = $this->RoleMenuAccess->forPage([
                        'wheres' => ['roleId' => $this->request->input('roleId')], 'page' => 1, 'limit' => 1000]);
                $arrMenuIds = array_unique($this->request->input('menuIds', []));
                $arrInserts = [];
                foreach ($arrMenuIds as $v) {
                        $arrInserts[] = [
                                'roleMenuId' => snowflakeId(),
                                'roleId' => $this->request->input('roleId'),
                                'menuId' => $v
                        ];
                }
                $this->RoleMenuAccess->delete(['wheres' => ['roleId' => $this->request->input('roleId')]]);
                $arrInserts && $this->RoleMenuAccess->insert($arrInserts);
                $this->createOperateLog($arrData, $arrInserts);
                return businessSuccess();
        }

        /**
         * 分页
         * @param array $inject
         * @return array
         */
        public function paginate(array $inject = []): array
        {
                $arrQueryResult = $this->RoleMenuAccess->paginate($inject);
                $arrResult = [
                        'page' => $arrQueryResult['current_page'],
                        'limit' => $arrQueryResult['per_page'],
                        'total' => $arrQueryResult['total'],
                        'list' => $arrQueryResult['data'],
                ];
                return businessSuccess($arrResult);
        }
}